﻿using System;
using System.Collections.Generic;
using System.Reflection;

namespace LeetCode
{
    class Program
    {
        static void Main(string[] args)
        {
            var assembly = Assembly.GetEntryAssembly();

            var list = new List<string>();
            foreach (var type in assembly.GetTypes())
            {
                var interfaces = new List<Type>(type.GetInterfaces());
                if (!interfaces.Contains(typeof(IProblem)))
                    continue;

                var name = type.ToString();
                name = name.Substring(name.Length - 4, 4);
                list.Add(name);
            }

            Console.WriteLine("可输出问题序号:");
            foreach (var name in list)
            {
                Console.Write(int.Parse(name) + " ");
            }
            Console.WriteLine();


            while (true)
            {
                Console.WriteLine("请输入问题序号:");
                int value = int.Parse(Console.ReadLine());
                var typeName = "LeetCode.P" + value.ToString("0000");
                var type = assembly.GetType(typeName);
                while (type == null)
                {
                    Console.WriteLine("没有找到该问题答案");
                    Console.WriteLine("请输入问题序号:");
                    value = int.Parse(Console.ReadLine());
                    typeName = "LeetCode.P" + value.ToString("0000");
                    type = assembly.GetType(typeName);
                }
                var p = assembly.CreateInstance(type.ToString()) as IProblem;
                p.Run();
            }

        }
    }
}
